<?
   class Broadway
   {
      private $SOAP      = ""; 
      private $ClientId  = "";
      private $AuthorizationCode = "";
      private $namespace = "http://tempuri.org/";
      private $xml;
      public  $new_york_city = "NYCA";
      public  $status_confirmed = "Confirmed";
      public  $status_error     = "error";
            
      function Broadway($BaseURL_str,$ClientId_str,$AuthorizationCode_str)
      {          
        $this->ClientId          = $ClientId_str;
        $this->AuthorizationCode = $AuthorizationCode_str;        
        $this->SOAP              = new nusoap_client($BaseURL_str,false);
        $this->SOAP->setCredentials($ClientId_str, $AuthorizationCode_str, 'basic');
        $this->initXML();
      }
     
      function initXML()
      {
          if(version_compare(PHP_VERSION,'5','>='))
          {
             $php_ver = "PHP5";
          }else
          {
             $php_ver = "PHP4";
          }
    
          $this->xml = new xml($php_ver);                  
      }
      
      function cdata($s)
      {
          return "<![CDATA[".$s."]]>";
      }
       
      function setAuthHeader()
      {
          $this->SOAP->setHeaders("<tem:AuthHeader><tem:username>".$this->ClientId."</tem:username><tem:password>".$this->AuthorizationCode."</tem:password></tem:AuthHeader>");
      }
      
      function isFault()
      {
          return $this->SOAP->fault;           
      }
      
      function getError()
      {
          return $this->SOAP->getError();
      } 
      
      function SOAPCall($func,$params)
      {
          $soapAction = $this->namespace.$func;
          $this->setAuthHeader();
          $this->SOAP->namespaces["tem"] = $this->namespace;
          return $this->SOAP->call($func,$params,$this->namespace,$soapAction);
      }
      
      function ShowBasics($SaleTypesCode,$ShowCityCode,$ShowAddedDate)
      {         
                $func       = "ShowBasics";
                $params = array(
    				'tem:SaleTypesCode' => $SaleTypesCode,
    				'tem:ShowCityCode'  => $ShowCityCode,
    				'tem:ShowAddedDate' => $ShowAddedDate
    			);
    			$this->SOAPCall($func,$params);
    			return $this->SOAP->responseData;
      }
      
      function ShowDetails($SaleTypesCode,$ShowCityCode,$ShowCodes,$LastChangeDate)
      {
                $func       = "ShowDetails";
                $params = array(
    				'tem:SaleTypesCode'  => $SaleTypesCode,
    				'tem:ShowCityCode'   => $ShowCityCode,
                	'tem:ShowCodes' 	 => $ShowCodes,                
    				'tem:LastChangeDate' => $LastChangeDate
    			);
    			
    			$this->SOAPCall($func,$params);
    			return $this->SOAP->responseData;          
      }
      
      function Performances($SaleTypesCode,$ShowCityCode,$DateBegins,$DateEnds,$ShowCodes,$AvailabilityType,$BestSeatsOnly,$LastChangeDate)
      {
                $func       = "Performances";
                $params = array(
    				'tem:SaleTypesCode'      => $SaleTypesCode,
    				'tem:ShowCityCode'       => $ShowCityCode,
                    'tem:DateBegins' 	     => $DateBegins,
                    'tem:DateEnds' 	         => $DateEnds,
                  	'tem:ShowCodes' 	     => $ShowCodes,                
                    'tem:AvailabilityType' 	 => $AvailabilityType,
                    'tem:BestSeatsOnly' 	 => $BestSeatsOnly,
    				'tem:LastChangeDate'     => $LastChangeDate
    			);

    			$this->SOAPCall($func,$params);
    			return $this->SOAP->responseData;          
      }

      function PerformancesPOHPricesAvailability($SaleTypesCode,$ShowCityCode,$DateBegins,$DateEnds,$OneShowCode,$AvailabilityType,$BestSeatsOnly,$LastChangeDate)
      {
                $func       = "PerformancesPOHPricesAvailability";
                $params = array(
    				'tem:SaleTypesCode'      => $SaleTypesCode,
    				'tem:ShowCityCode'       => $ShowCityCode,
                    'tem:DateBegins' 	     => $DateBegins,
                    'tem:DateEnds' 	         => $DateEnds,
                  	'tem:OneShowCode' 	     => $OneShowCode,                
                    'tem:AvailabilityType' 	 => $AvailabilityType,
                    'tem:BestSeatsOnly' 	 => $BestSeatsOnly,
    				'tem:LastChangeDate'     => $LastChangeDate
    			);

    			$this->SOAPCall($func,$params);
    			return $this->SOAP->responseData;
      }
      
      function CityList()
      {
                $func       = "CityList";
    			$params = "";
    			$this->SOAPCall($func,$params);
    			return $this->SOAP->responseData;
      }
      
      
      /*
       *  Send new order to BI
       *  SaleTypesCode  - "F" for single customer or "G" for groups
       *  ProductId - product id. for example 69058
       *  OneShowCode - show code. for example "PHANTOM"
       *  EventDateTime - date and time. format: yyyy-mm-ddThh:mm:ss.0Z 
       *  
       * 
       */      
      function NewOrder($SaleTypesCode,$ProductId,$OneShowCode,$EventDateTime,$Quantity,$Price,$BookingLastName,$BookingFirstName,$BookingReferenceNumber,$BookingNotes)
      {
                $func       = "NewOrder";
                $params = array(
    				'tem:SaleTypesCode'      => $SaleTypesCode,
    				'tem:ProductId'          => $ProductId,
                    'tem:OneShowCode' 	     => $OneShowCode,
                    'tem:EventDateTime' 	 => $EventDateTime,
                  	'tem:Quantity' 	         => $Quantity,                
                    'tem:Price' 			 => $Price,
                    'tem:BookingLastName' 	 => $this->cdata($BookingLastName),
                	'tem:BookingFirstName' 	 => $this->cdata($BookingFirstName),
    				'tem:BookingReferenceNumber' => $BookingReferenceNumber,
                	'tem:BookingNotes' 	     => $this->cdata($BookingNotes)
    			);

    			$this->SOAPCall($func,$params);
    			return $this->SOAP->responseData;                    
      }
            
//end of functions....below parsing of results=================================
      
      function parseShowBasics($res)
      {
          $doc  = $this->xml->loadFromString($res);
          $root = $this->xml->getRoot($doc);

          $res = array();
          $is_first = true;
          foreach ($this->xml->getElements($root,"Table") as $Table)
          {
              if($is_first){ $is_first=false; continue; } //skip first
              
              $show = new Show();              
              $show->ShowCode     = trim($this->xml->getElementValueByName($Table,"ShowCode"));
              $show->ShowName     = trim($this->xml->getElementValueByName($Table,"ShowName"));
              $show->ShowAreaCode = trim($this->xml->getElementValueByName($Table,"ShowAreaCode"));
              $show->ShowTypeCode = trim($this->xml->getElementValueByName($Table,"ShowTypeCode"));
              $res[$show->ShowCode] = $show;
          }
          return $res;
      }
      
      //parse show details xml
      //return array of show objects
      function parseShowDetails($show_details_xml)
      {
          $doc  = $this->xml->loadFromString($show_details_xml);
          $root = $this->xml->getRoot($doc);

          $res = array();
          foreach ($this->xml->getElements($root,"Table") as $Table)
          {
              $show = new Show();
              $show->ShowCode            = trim($this->xml->getElementValueByName($Table,"ShowCode"));
              $show->ShowName            = trim($this->xml->getElementValueByName($Table,"ShowName"));
              $show->ShowAreaCode        = trim($this->xml->getElementValueByName($Table,"ShowAreaCode"));
              $show->ShowTypeCode        = trim($this->xml->getElementValueByName($Table,"ShowTypeCode"));
              $show->VenueName           = trim($this->xml->getElementValueByName($Table,"VenueName"));
              $show->Address             = trim($this->xml->getElementValueByName($Table,"Address"));
              $show->City                = trim($this->xml->getElementValueByName($Table,"City"));
              $show->StateCode           = trim($this->xml->getElementValueByName($Table,"StateCode"));
              $show->CountryCode         = trim($this->xml->getElementValueByName($Table,"CountryCode"));
              $show->BoOpensDate         = trim($this->xml->getElementValueByName($Table,"BoOpensDate"));
              $show->FirstPreviewDate    = trim($this->xml->getElementValueByName($Table,"FirstPreviewDate"));
              $show->OpeningDate         = trim($this->xml->getElementValueByName($Table,"OpeningDate"));
              $show->RunTime             = trim($this->xml->getElementValueByName($Table,"RunTime"));
              $show->RatingCode          = trim($this->xml->getElementValueByName($Table,"RatingCode"));
              $show->Synopsis            = trim($this->xml->getElementValueByName($Table,"Synopsis"));                
              $res[$show->ShowCode] = $show;
          }
          return $res;
      }

      //only one date and one show
      //return list of available products for given date/show
      function parsePOH($show_poh)
      {
          $doc  = $this->xml->loadFromString($show_poh);
          $root = $this->xml->getRoot($doc);

          $res = array();
          foreach ($this->xml->getElements($root,"Table") as $Table)
          {
              $product = new AvailableProduct();
              $product->ProductId       = trim($this->xml->getElementValueByName($Table,"ProductId"));
              $product->ProductCode     = trim($this->xml->getElementValueByName($Table,"ProductCode"));
              $product->ProductDate     = trim($this->xml->getElementValueByName($Table,"ProductDate"));
              $product->ProductTime     = trim($this->xml->getElementValueByName($Table,"ProductTime"));
              $product->Description     = trim($this->xml->getElementValueByName($Table,"Description"));
              $product->Price           = trim($this->xml->getElementValueByName($Table,"Price"));
              $product->RegularPrice    = trim($this->xml->getElementValueByName($Table,"RegularPrice"));
              $product->Currency        = trim($this->xml->getElementValueByName($Table,"Currency"));
              $product->BestSeats       = trim($this->xml->getElementValueByName($Table,"BestSeats"));
              $product->Availability    = trim($this->xml->getElementValueByName($Table,"Availability"));
              $res[$product->ProductId] = $product;
          }
          return $res;
      }     

      //only for 1 item per order! 
      function parseNewOrder($new_order)
      {
          $doc  = $this->xml->loadFromString($new_order);
          $root = $this->xml->getRoot($doc);

          $res = array();
          $is_error = false;
          foreach ($this->xml->getElements($root,"Table1") as $Table1)
          {
              $is_error = true;
              $msg = trim($this->xml->getElementValueByName($Table1,"Error"));
              if($msg=="")
              {
                  $msg = trim($this->xml->getElementValueByName($Table1,"DaterangeError"));
              }
              $res["Status"] = $this->status_error;
              $res["Msg"]    = $msg;
              return $res;
          }
          
          foreach ($this->xml->getElements($root,"Outbound") as $Outbound)
          {
              $res["Status"] = trim($this->xml->getElementValueByName($Outbound,"Status"));
              $res["Product"] = trim($this->xml->getElementValueByName($Outbound,"Product"));
              $res["OrderID"] = trim($this->xml->getElementValueByName($Outbound,"OrderID"));              
              $res["Price"] = trim($this->xml->getElementValueByName($Outbound,"Price"));
              $res["Quantity"] = trim($this->xml->getElementValueByName($Outbound,"Quantity"));
              $res["Description"] = trim($this->xml->getElementValueByName($Outbound,"Description"));              
              $res["BookingReferenceID"] = trim($this->xml->getElementValueByName($Outbound,"BookingReferenceID"));
              $res["Currency"] = trim($this->xml->getElementValueByName($Outbound,"Currency"));
              $res["Record"] = trim($this->xml->getElementValueByName($Outbound,"Record"));                                          
          }
          return $res;
      }     
      
/*      
      	        <Outbound diffgr:id="Outbound1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
          	        <Product>SOUTHPAC</Product>
          	        <OrderID>328103</OrderID>
          	        <Status>Confirmed</Status>
          	        <Price>137.5</Price>
          	        <Quantity>2</Quantity>
          	        <Description>Orchestra/ Loge A-C</Description>
          	        <BookingReferenceID>OrderID_01</BookingReferenceID>
          	        <Currency>USD</Currency>
          	        <Record>New</Record>
      	        </Outbound>
*/  

/*
 error
	<Table1 diffgr:id="Table11" msdata:rowOrder="0" diffgr:hasChanges="inserted">
		<DaterangeError>EventDate must be 12/10/2009 or later</DaterangeError>
	</Table1>
*/

/* error
	<Table1 diffgr:id="Table11" msdata:rowOrder="0" diffgr:hasChanges="inserted">
		<Error>Your input returned no data. Booking Reference Number : OrderID_0</Error>
	</Table1>
*/      
      
}
?>
<?php
   class Show
   {
       var $ShowCode;
       var $ShowName;
       var $ShowAreaCode;
       var $ShowTypeCode;
       var $VenueName;
       var $Address;
       var $City;
       var $StateCode;
       var $CountryCode;
       var $BoOpensDate;
       var $FirstPreviewDate;
       var $OpeningDate;
       var $RunTime;
       var $RatingCode;
       var $Synopsis;
   }
?>
<?php
   class AvailableProduct
   {
      var $ProductId; 
      var $ProductCode; 
      var $ProductDate; 
      var $ProductTime; 
      var $Description; 
      var $Price; 
      var $RegularPrice; 
      var $Currency; 
      var $BestSeats; 
      var $Availability;    
   }
?>